package com.whosly.strategy.scan.spring;

import com.whosly.strategy.IStrategy;
import com.whosly.strategy.container.IStrategyContainer;
import com.whosly.strategy.scan.ContainerProxy;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * 会被提前加载
 *
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-05 15:26
 */
public class ContainerFactoryBean<C, S extends IStrategy<C>, T extends IStrategyContainer<C, S>>
        implements FactoryBean<T> {
    /**
     * 策略容器
     */
    private Class<IStrategyContainer<C, S>> strategyContainerInterface;

    public void setStrategyContainerInterface(Class<IStrategyContainer<C, S>> strategyContainerInterface) {
        this.strategyContainerInterface = strategyContainerInterface;
    }

    @Override
    public T getObject() throws Exception {
        return (T) Proxy.newProxyInstance(strategyContainerInterface.getClassLoader(),
                new Class[]{strategyContainerInterface},
                new ContainerProxy<>(strategyContainerInterface));
    }

    @Override
    public Class<IStrategyContainer<C, S>> getObjectType() {
        return this.strategyContainerInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
